package com.lss.myapplication.util;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.lss.myapplication.db.City;
import com.lss.myapplication.db.County;
import com.lss.myapplication.db.Province;
import com.lss.myapplication.db.database.AppDatabase;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Utility {
    public static boolean handleProvinceResponse(String response, Context context) throws JSONException {
        if (!TextUtils.isEmpty(response)) {
            try {
                JSONArray allProvinces = new JSONArray(response);
                for (int i = 0; i < allProvinces.length(); i++) {
                    JSONObject provinceObject = allProvinces.getJSONObject(i);
                    Province province = new Province(provinceObject.getInt("id"),
                            provinceObject.getString("name")
                           );
                    AppDatabase.getInstance(context).provinceDao().insert(province);
                }
            } catch (JSONException e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }

//    public static boolean handleCityResponse(String response, Context context) {
//        if (!TextUtils.isEmpty(response)) {
//            try {
//                JSONArray allCities = new JSONArray(response);
//                for (int i = 0; i < allCities.length(); i++) {
//                    JSONObject cityObject = allCities.getJSONObject(i);
//                    City city = new City(cityObject.getInt("id"),
//                            cityObject.getString("cityName"),
//                            cityObject.getString("cityCode"),
//                            cityObject.getInt("provinceId"));
//                    AppDatabase.getInstance(context).cityDao().insert(city);
//                }
//
//            } catch (JSONException e) {
//                e.printStackTrace();
//                return false;
//            }
//        }
//        return true;
//    }

//    public static boolean handleCountiesResponse(String response, Context context) {
//        if (!TextUtils.isEmpty(response)) {
//            try {
//                JSONArray allCounties = new JSONArray(response);
//                for (int i = 0; i < allCounties.length(); i++) {
//                    JSONObject countyObject = allCounties.getJSONObject(i);
//                    County county = new County(countyObject.getInt("id"),
//                            countyObject.getString("countyName"),
//                            countyObject.getString("countyCode"),
//                            countyObject.getInt("cityId"));
//                    AppDatabase.getInstance(context).countyDao().insert(county);
//                }
//            } catch (JSONException e) {
//                e.printStackTrace();
//                return false;
//            }
//        }
//        return true;
//    }


    public static boolean handleCityResponse(String response, int provinceId, Context context) {
        if (!TextUtils.isEmpty(response)) {
            try {
                JSONArray allCities = new JSONArray(response);
                for (int i = 0; i < allCities.length(); i++) {
                    JSONObject cityObject = allCities.getJSONObject(i);
                    City city = new City(cityObject.getInt("id"),
                            cityObject.getString("name"),
                            cityObject.getString("cityCode"),
                            provinceId);
                    AppDatabase.getInstance(context).cityDao().insert(city);
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /**
     * 解析和处理服务器返回的县级数据
     */
    public static boolean handleCountyResponse(String response, int cityId, Context context) {
        if (!TextUtils.isEmpty(response)) {
            try {
                JSONArray allCounties = new JSONArray(response);
                for (int i = 0; i < allCounties.length(); i++) {
                    JSONObject countyObject = allCounties.getJSONObject(i);
                    County county = new County(countyObject.getInt("id"),
                            countyObject.getString("name"),
                            countyObject.getString("countyCode"),
                            cityId);
                    AppDatabase.getInstance(context).countyDao().insert(county);
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /**
     * 将返回的JSON数据解析成Weather实体类
     */
//    public static Weather handleWeatherResponse(String response) {
//        try {
//            JSONObject jsonObject = new JSONObject(response);
//            JSONArray jsonArray = jsonObject.getJSONArray("HeWeather");
//            String weatherContent = jsonArray.getJSONObject(0).toString();
//            return new Gson().fromJson(weatherContent, Weather.class);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
}
